package com.yhp.servlet;


import com.yhp.bean.Student;
import com.yhp.service.impl.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/getStudentList")
public class StudentsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数
        //1.1模糊查找
        String name = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        sex = sex==null? "-1":sex;//防止空异常
        //1.2分页数据 linit 开始位置，显示条数
        String pageindex = req.getParameter("pageIndex");
        int index = pageindex==null? 1:Integer.parseInt(pageindex);//如果为空则默认为第一页
        //2.调取service
        //2.1获取学生信息
        List<Student> students =
                StudentService.getstudent(name,stuno,Integer.parseInt(sex),index,5);
        //2.2获取总页数 = 总条数%显示数+1
        int total = StudentService.total(name,stuno,Integer.parseInt(sex));
        int totalPage = total%5>0?total/5+1:total/5;
        //3.存入数据跳转页面
        //3.1存入搜索记录
        req.setAttribute("name",name);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //3.2存入当前页码数
        req.setAttribute("index",index);
        req.setAttribute("size",5);
        req.setAttribute("total",total);
        req.setAttribute("totalPage",totalPage);
        //3.3存入学生信息
        req.getSession().setAttribute("students",students);
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }
}
